package cn.ondraw.ragflow.service.impl;


import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.request.CreateDatasetRequest;
import cn.ondraw.ragflow.pojo.request.DeleteDatasetsRequest;
import cn.ondraw.ragflow.pojo.request.ListDatasetsRequest;
import cn.ondraw.ragflow.pojo.request.UpdateDatasetRequest;
import cn.ondraw.ragflow.pojo.response.CreateDatasetResponse;
import cn.ondraw.ragflow.pojo.response.DeleteDatasetsResponse;
import cn.ondraw.ragflow.pojo.response.ListDatasetsResponse;
import cn.ondraw.ragflow.pojo.response.UpdateDatasetResponse;
import cn.ondraw.ragflow.service.DatasetService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class DatasetServiceImpl implements DatasetService {

    private final PythonApiCaller pythonApiCaller;

    private final String URL_PREFIX = "/api/v1/datasets";

    /**
     * 创建新数据集
     *
     * @param request 创建数据集请求对象
     * @return 包含操作结果的响应实体
     */
    @Override
    public CreateDatasetResponse createDataset(CreateDatasetRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CreateDatasetResponse.class, true);
    }

    /**
     * 删除数据集
     *
     * @param request 删除数据集请求对象
     * @return 包含操作结果的响应实体
     */
    @Override
    public DeleteDatasetsResponse deleteDatasets(DeleteDatasetsRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callDeleteApi(URL_PREFIX, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, DeleteDatasetsResponse.class, true);
    }

    /**
     * 更新数据集
     *
     * @param datasetId 数据集 ID
     * @param request   更新数据集请求对象
     * @return 包含操作结果的响应实体
     */
    @Override
    public UpdateDatasetResponse updateDataset(String datasetId, UpdateDatasetRequest request) {
        // 构建请求的 URL
        String url = URL_PREFIX + "/" + datasetId;
        ResponseEntity<?> response = pythonApiCaller.callPutApi(url, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, UpdateDatasetResponse.class, true);
    }

    /**
     * 列出数据集
     *
     * @param request 列出数据集请求对象
     * @return 包含操作结果的响应实体
     */
    @Override
    public ListDatasetsResponse listDatasets(ListDatasetsRequest request) {
        StringBuilder urlBuilder = new StringBuilder(URL_PREFIX + "?");
        if (request.getId() != null) {
            urlBuilder.append("id=").append(request.getId()).append("&");
        }
        if (request.getName() != null) {
            urlBuilder.append("name=").append(request.getName()).append("&");
        }
        if (request.getPage() != null) {
            urlBuilder.append("page=").append(request.getPage()).append("&");
        }
        if (request.getPage_size() != null) {
            urlBuilder.append("page_size=").append(request.getPage_size()).append("&");
        }
        if (request.getOrderby() != null) {
            urlBuilder.append("orderby=").append(request.getOrderby()).append("&");
        }
        if (request.getDesc() != null) {
            urlBuilder.append("desc=").append(request.getDesc()).append("&");
        }
        if (urlBuilder.charAt(urlBuilder.length() - 1) == '&') {
            urlBuilder.deleteCharAt(urlBuilder.length() - 1);
        }
        String url = urlBuilder.toString();
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, ListDatasetsResponse.class, true);
    }
}
